package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.heima.article.mapper.AuthorMapper;
import com.heima.article.service.AuthorService;
import com.heima.model.article.pojos.ApAuthor;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class AuthorServiceImpl extends ServiceImpl<AuthorMapper, ApAuthor> implements AuthorService {
    @Override
    public ResponseResult insert(ApAuthor apAuthor) {
        //判断参数
        if (apAuthor==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //根据apAuthor储存
        this.save(apAuthor);
        return ResponseResult.okResult();
    }

    @Override
    public ApAuthor findByUserId(Integer id) {
        //判断参数
        if (id==null){
            return null;
        }
        //创建查询条件
        LambdaQueryWrapper<ApAuthor> queryWrapper = Wrappers.<ApAuthor>lambdaQuery();
        queryWrapper.eq(ApAuthor::getUserId, id);
        List<ApAuthor> list = this.list(queryWrapper);
        //判断返回值是否为空
        if (list!=null&&!list.isEmpty()){
            return list.get(0);
        }
        return null;

    }
}
